package com.itheima.controller;

import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.User;
import com.itheima.pub.MessageConstant;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    UserService userService;

    @RequestMapping("getUserName")
    public Result getUserName(Authentication authentication){
        try {
            String username = authentication.getName();
            return new Result(true,null,username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取用户名失败");
        }
    }

    @RequestMapping("changePassword")
    public Result changePassword(@RequestParam("username") String username,@RequestParam("password") String password){
        try {
            userService.changePassword(username,password);
            return new Result(true,"修改密码成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改密码失败");
        }
    }

    // 此方法用作模板引擎登录
    @RequestMapping("login")
    public ModelAndView login(String error){
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("---login---");
        modelAndView.addObject("error",error);
        modelAndView.setViewName("mylogin");
        return  modelAndView;
    }

    @RequestMapping("backViewForLogin.do")
    public Result backViewForLogin(){
        try {
            Map<String,Object> map = userService.backViewForLogin();
            return new Result(true,"登录信息回显成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"登录信息回显失败");
        }
    }


    //user表单部分

    // 分页查询检查项
    @RequestMapping("findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map page = userService.findPage(queryPageBean);
            return new Result(true,"后台用户分页查询成功！",page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"后台用户分页查询失败！");
        }
    }

    // 添加或者更新检查项
    @RequestMapping("addUser.do")
    public Result addCheckItem(@RequestBody User user){
        try {
            // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新
            userService.saveOrUpdateByUser(user);
            return new Result(true,"添加后台用户成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加后台用户失败！");
        }
    }

    // 根据id值删除检查项
    @RequestMapping("deleteUserById.do")
    public Result deleteUserById(@RequestParam("id") Integer id){
        try {
            userService.deleteUserById(id);
            return new Result(true, "删除后台用户成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除后台用户失败！");
        }
    }


}
